
$(function () {
    var btns = document.querySelectorAll('.btn');
    var toggleBar=document.querySelectorAll('.toggle_bar');
    var inquire=document.querySelector('#inquire');
    var approve=document.querySelector('#approve');
    var sideSwipeWrap=document.querySelectorAll('.sideSwipe')[0];
    var cancelBtn=document.querySelector('#swipe_cancel_btn');
    var approveBtn=document.querySelector('#approve');
    var rejectBtn=document.querySelector('#reject');
    var remarks =document.querySelector('#remarks');
    var isInquire=true;
    var isReject=true;
    function sideSwipe() {
        sideSwipeWrap.addClass('in');
        remarks.focus();
        uiFix();
    }
    function preventDfu(event) {
        event.preventDefault();
    }

    function uiFix() {
        document.body.style.overflow='hidden';
        document.addEventListener('touchmove', preventDfu);
    }

    function uiAuto() {
        document.body.style.overflow='';
        document.removeEventListener('touchmove', preventDfu);
    }
    inquire.onclick=function () {
        isReject=false;
        if(!isInquire){
            remarks.value="";
            remarks.removeClass('warning');
        }
        isInquire=true;
        sideSwipe();
    }
    approveBtn.onclick=function () {
        //ajax 提交 成功后可以调用
        common_popupWindow({
            content: "提交成功",
            btnYes: {
                text: "确定",
                url: "javascript:void(0)",
                callback: function () {
                    //回调



                }
            }
        })
    }
    rejectBtn.onclick=function () {
        isInquire=false;
        if(!isReject){
            remarks.value="";
            remarks.removeClass('warning');
        }
        isReject=true;
        sideSwipe();
    }
    cancelBtn.onclick= function () {
        sideSwipeWrap.removeClass('in');
        uiAuto();
    }

    /*询问*/
    document.querySelector('#sideSwipe_btn').onclick=function () {
        if ($.trim($(remarks).val()) == "") {
            remarks.addClass('waring');
        } else {
            //ajax 提交 成功后可以调用
            common_popupWindow({
                content: "提交成功",
                btnYes: {
                    text: "确定",
                    url: "javascript:void(0)",
                    callback: function () {
                        //回调



                    }
                }
            })

        }
    };

    remarks.onfocus=function () {
        this.removeClass('waring');
    }

/*弹层方法*/
    function common_popupWindow(/*p,*/ option) {

        function insertStyle(cssSrc) {
            var css = document.createElement("link");
            css.rel = "stylesheet";
            css.href = cssSrc;
            document.head.appendChild(css)
        };
        function getLocationPopup() {
            /*  if (!p) {
             return
             }
             popup.insertStyle(p);*/
            var setting = option || {
                    title: "默认标题",
                    closeBtn: false,
                    content: "描述文字。。。",
                    btnYes: {text: "确定", url: ""},
                    btnNo: {text: "取消", url: ""}
                };
            if (setting.closeBtn) {
                var closeBtn = '<b class="close"></b>';
            } else {
                var closeBtn = "";
            }
            if (setting.hasOwnProperty("title")) {
                var title = '<div class="title">' + setting.title + '</div>';
            } else {
                var title = "";
            }
            if (setting.hasOwnProperty("btnYes")) {
                var btnYes = '<a href="' + setting.btnYes.url + '" class="btn_public btnYes">' + setting.btnYes.text + "</a>"
            } else {
                var btnYes = ""
            }
            if (setting.hasOwnProperty("btnNo")) {
                var btnNo = '<a href="' + setting.btnNo.url + '" class="btn_public btnNo">' + setting.btnNo.text + "</a>"
            } else {
                var btnNo = ""
            }
            var pop = '<div class="popup_wrap">' + title + closeBtn + '<div class="text">' + setting.content + '</div><div class="btn_box">' + btnNo + btnYes + "</div></div>";
            var div = document.createElement("div");
            div.id = "popupTipsLocation";
            div.innerHTML = pop;
            document.body.appendChild(div);
            var popupWrap = div.querySelector(".popup_wrap");
            setTimeout(function () {
                popupWrap.className += " popin";
            }, 100)
            div.querySelector(".close") && close.addEventListener("click", function () {
                closePopup()
            }, false);
            div.querySelector(".btnNo") && div.querySelector(".btnNo").addEventListener("click", function () {
                closePopup();
            }, false);
            div.querySelector(".btnYes") && div.querySelector(".btnYes").addEventListener("click", function () {
                if (setting.btnYes.callback) {
                    (setting.btnYes.callback)();
                    closePopup();
                }
            }, false);

            function closePopup() {
                var node = document.getElementById('popupTipsLocation');
                popupWrap.className += ' popout';
                setTimeout(function () {
                    removeElement(node);
                }, 200)
            };
        };

        getLocationPopup();
    };

    for (var i = 0; i < btns.length; i++) {
        var fn = function (j) {
            btns[j].onclick = function () {
                var tr = getNextElement(this.parentNode.parentNode);
                if (!this.hasClass('open')) {
                    this.addClass('open');
                    tr.style.display = 'table-row';
                    this.innerHTML = '-';
                } else {
                    this.removeClass('open');
                    tr.style.display = 'none';
                    this.innerHTML = '+';
                }
            }
        }
        fn(i);
    }


    /*区块显示隐藏*/
    for (var i = 0; i < toggleBar.length; i++) {
        var fn = function (j) {
            toggleBar[j].onclick = function () {
                var bd = getNextElement(this);
                if (!this.hasClass('open')) {
                    this.addClass('open');
                    bd.style.display = 'block';
                } else {
                    this.removeClass('open');
                    bd.style.display = 'none';
                }
            }
        }
        fn(i);
    }


    function removeElement(_element){
        var _parentElement = _element.parentNode;
        if(_parentElement){
            _parentElement.removeChild(_element);
        }
    }
    function getNextElement(node) {
        var NextElementNode = node.nextSibling;
        while (NextElementNode.nodeValue != null) {
            NextElementNode = NextElementNode.nextSibling
        }
        return NextElementNode;
    }

    Object.prototype.hasClass= function (cls) {
        return this.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
    }

    Object.prototype.addClass= function (cls) {
        if (!this.hasClass(cls)) this.className += " "+cls;
    }

    Object.prototype.removeClass= function (cls) {
        var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
        this.className=this.className.replace(reg,'');
    }
})